//
//  TypeMovieViewController.m
//  UI豆瓣2015到2016跨年作业
//
//  Created by LHY on 16/1/4.
//  Copyright © 2016年 Lanou 32 李昊宇. All rights reserved.
//

#import "TypeMovieViewController.h"
#import "Layout.h"
#import "ModelMovie.h"
#import "CollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MovieDetailsViewController.h"
@interface TypeMovieViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic , retain) NSMutableArray *arrModel;



@end

@implementation TypeMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"电影";
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"/Users/dllo/Desktop/UI豆瓣2015到2016跨年作业/UI豆瓣2015到2016跨年作业/btn_nav_list@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal/**保证图片不被渲染 */] style:UIBarButtonItemStylePlain target:self action:@selector(action1:)];
    self.navigationItem.rightBarButtonItem = item3;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStylePlain) target:self action:nil];
    self.navigationItem.leftBarButtonItem = item;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor clearColor];
    
    
        
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 150);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.headerReferenceSize = CGSizeMake(100, 0);
    flowLayout.footerReferenceSize = CGSizeMake(100, 100);
    
   
    
    
    
    UICollectionView *collectionview = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    collectionview.delegate = self;
    collectionview.dataSource = self;
    collectionview.backgroundColor = [UIColor whiteColor];
    
    
    [collectionview registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.view addSubview:collectionview];
    
    [self data];
    
}

- (void)data {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    self.arrModel = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        ModelMovie *mod = [ModelMovie modelMovie];
        [mod setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:mod];
    }
    NSLog(@"%@", _arrModel);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieDetailsViewController *mov = [[MovieDetailsViewController alloc] init];
    mov.index = indexPath;
    
    [self.navigationController pushViewController:mov animated:YES];
    [mov release];

    
    
    


}


- (void)action1:(UIBarButtonItem *)action {

    [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];

};

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _arrModel.count;
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor orangeColor];
    ModelMovie *mod = _arrModel[indexPath.row];
    cell.label.text = mod.title;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:mod.image]];
    
    
    
    return cell;
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
